package com.gjm.demo.controller;

import com.gjm.demo.model.MessageInfo;
import com.gjm.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    @Autowired
    private MessageService messageService;

    //获取所有留言
    @RequestMapping("/getList")
    public List<MessageInfo> getList() {
        return messageService.queryAllMessage();
    }

    //添加留言
    @RequestMapping(value = "/publish", produces = "application/json")//返回json数据
    public String publish(@RequestBody MessageInfo messageInfo) {//前端使用from表单传数据，就不需要加上@RequestBody，若使用json传数据，就要加上
        //留言不完整
        if (!StringUtils.hasLength(messageInfo.getFrom()) ||
            !StringUtils.hasLength(messageInfo.getTo()) ||
            !StringUtils.hasLength(messageInfo.getMessage())) {
            System.out.println(messageInfo.getFrom());
            System.out.println(messageInfo.getTo());
            System.out.println(messageInfo.getMessage());
            return "{\"ok\" : 0}";
        }

        messageService.addMessage(messageInfo);

        return "{\"ok\": 1}";
    }

}
